const api = require("../../utils/api");
const Page = require('../../utils/pageProxy');

Page({
    loginRequired: true,
    data: {
        shopId: 0,
        storeInfo: '',

        amount: '',
        isFocus: false,

        orderId: 0,
        orderDetail: ''
    },
    onLoad(options) {
        console.log(options)
        if(options.scene != undefined) {
            this.setData({
                shopId: options.scene
            });
            this.getStoreInfo();
        } else if(options.shopId != undefined) {
            this.setData({
                shopId: parseInt(options.shopId)
            });
            this.getStoreInfo();
        }
    },
    onShow() {
        // this.setData({
        //     isFocus: true
        // });
    },
    getStoreInfo() { //获取提货店铺信息
        api.post('/shop/detail?shopId=' + this.data.shopId)
            .then(res => {
                this.setData({
                    storeInfo: res,
                    isFocus: true
                });
            })
    },

    bindinput(e) { //输入框监听
        let item = e.currentTarget.dataset.item;
        let val = e.detail.value;

        this.setData({
            [item]: val
        });
    },

    submitOrder() { //提交收银订单
        if(this.data.amount == '' || this.data.amount == 0) {
            wx.showToast({
                title: '请输入支付金额',
                icon: 'error',
            });
            return;
        }
        if(parseFloat(this.data.amount) < 0) {
            wx.showToast({
                title: '请输入正确的金额',
                icon: 'error',
            });
            return;
        }
        wx.showLoading({
            title: '提交中...',
        });
        let params = {
            shopId: this.data.storeInfo.id, //店铺ID，系统不支持跨店铺下单
            shopNo: this.data.storeInfo.shopNo, //店铺编号
            orderType: 5, //订单类型: 1零售订单、2商城自提、3商城物流、5线下收款
            payableAmount: parseFloat(this.data.amount), //应付金额
            orderTotalAmount: parseFloat(this.data.amount),
            balingCharges: 0, //打包费
            isMemberPay: 0,
            couponAmount: 0,
            couponSyncBuy: 0, //同步购券的总面额，没有则填0
        }
        api.post('/order/submit', params)
            .then(res => {
                this.setData({
                    orderId: res,
                });
                setTimeout(() => {
                    this.payOrder();
                    this.getOrderDetail();
                }, 800);
            })
    },
    payOrder() {
        api.post('/payment/ccb/pay', {
            orderId: this.data.orderId,
            payableAmount: parseFloat(this.data.amount), //应付金额
            couponAmount: 0,
            payMethod: '',
            q: 0,
        })
            .then(res => {
                //调起微信支付
                let data = JSON.parse(res);
                this.ccbPay(data);
            })
    },
    ccbPay(res) { //调起建行支付
        wx.requestPayment({
            timeStamp: res.timeStamp,
            nonceStr: res.nonceStr,
            package: res.package,
            signType: res.signType,
            paySign: res.paySign,
            success: () => {
                this.checkWeixinPayResult();
            },
            fail: () => {
                wx.showToast({
                    title: '支付失败，请重试！',
                    icon: 'none'
                });
            }
        })
    },

    getOrderDetail() { //获取商城订单详情
        api.post(`/order/detail?orderId=${this.data.orderId}`)
            .then(res => {
                this.setData({
                    orderDetail: res,
                });
            })
    },
    checkWeixinPayResult() { //检查微信支付结果
        api.post(`/payment/ccb/order/query?orderNo=${this.data.orderDetail.orderNo}`)
            .then(res => {
                let status = res;
                switch(status) {
                    case 'SUCCESS':
                        wx.showToast({
                            title: '支付成功！',
                            icon: 'success'
                        });
                        setTimeout(() => {
                            wx.switchTab({
                                url: '/pages/user/user',
                            })
                        }, 1000);
                        break;
                    case 'FAIL':
                        wx.showToast({
                            title: '支付失败，请重试！',
                            icon: 'none'
                        });
                        break;
                    case 'CANCEL':
                        wx.showToast({
                            title: '支付已取消！',
                            icon: 'none'
                        });
                        break;
                    case 'WAIT_PAY':
                        setTimeout(() => {
                            this.checkPayResult();
                        }, 500);
                        break;
                }
            })
    },
})